﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CloudScroller : MonoBehaviour {

	GameObject singletonObjects;
	GameObject instantiatedClouds;
	GameObject rightWall;
	GameObject marioCloud;
	SpriteRenderer [] renderers;
	GameObject [,] objs;
	
	public float [] cloudTimers;
	public double TimeIntervalMax;
	public double TimeIntervalMin;
	public bool Disabled = false;

	public void Reset() {
		this.singletonObjects = GameObject.Find ("SingletonObjects");
		this.instantiatedClouds = GameObject.Find ("InstantiatedClouds");
		this.rightWall = this.singletonObjects.transform.FindChild ("RightWall").gameObject;
		this.marioCloud = this.singletonObjects.transform.FindChild ("MarioCloud1").gameObject;

		if (this.singletonObjects == null) {
			this.singletonObjects = GameObject.Find ("SingletonObjects");
		}
		if (this.rightWall == null) {
			this.rightWall = this.singletonObjects.transform.FindChild ("RightWall").gameObject;
		}
		if (this.marioCloud == null) {
			this.marioCloud = this.singletonObjects.transform.FindChild ("MarioCloud1").gameObject;
		}

		this.renderers = this.instantiatedClouds.GetComponentsInChildren<SpriteRenderer> ();

		this.cloudTimers = new float[10];
		this.objs = new GameObject[6,10];
		
		for (int z=0; z<10; z++) {
			int y = 0;
			cloudTimers[z] = (float)Mathf.Lerp((float)TimeIntervalMin, (float)TimeIntervalMax, 0.1f * (float)z);
			foreach(SpriteRenderer ren in this.renderers) {
				if ((ren.transform.name == "MarioCloudScrolled2_" + z + "_A") || (ren.transform.name == "MarioCloudScrolled2_" + z + "_B")) {
					this.objs[y,z] = ren.gameObject;
					y++;
				}
			}

			/*
			GameObject g = renderers[z].gameObject;
			float xsize = this.renderers[z].sprite.bounds.size.x;
			float ypos = g.transform.position.y;
			float xposInFront = g.transform.position.x - xsize;
			float xposBehind = g.transform.position.x + xsize;
			float zpos = g.transform.position.z;
			Vector3 frontPoint = new Vector3(xposInFront, ypos, zpos);
			Vector3 behindPoint = new Vector3(xposBehind, ypos, zpos);

			GameObject newObject = (GameObject)Instantiate(g, frontPoint, Quaternion.identity);
			newObject.transform.parent = this.instantiatedClouds.transform;

			GameObject newObject2 = (GameObject)Instantiate(g, behindPoint, Quaternion.identity);
			newObject2.transform.parent = this.instantiatedClouds.transform;
			*/

			//Debug.Log(this.renderers[z].gameObject.name);
			//Debug.Log();
		}
	}
	// Use this for initialization
	void Start () {
		this.Reset ();
	}

	// Update is called once per frame
	void Update () {
	}

	void FixedUpdate() {
		if (this.Disabled) {
			return;
		}
		for (int z=0; z<10; z++) {
			//foreach(SpriteRenderer ren in this.renderers) {
			//	if (ren.gameObject.name == "MarioCloudScrolled2_" + z) {
					//ren.transform.position -= new Vector3((float)cloudTimers[z],0f,0f);
			for (int y=0;y<6;y++) {
				this.objs[y,z].transform.position -= new Vector3(cloudTimers[z],0f,0f);
			}
			//	}
			//}
			//renderers[z].
			
			//cloudTimers[z] -= Time.deltaTime;
			//if (cloudTimers[z] < 0f) {
			//cloudTimers[z] = (double)Mathf.Lerp((float)TimeIntervalMin, (float)TimeIntervalMax, 0.1f * (float)z);
			
			
			/*
				Vector3 startPoint = new Vector3(this.rightWall.transform.position.x, (float)z * 0.5f - 0f, 0f - (float)z + 10f);
				Vector3 startPoint2 = startPoint;
				startPoint2.y = -1f * startPoint2.y;

				GameObject newObject = (GameObject)Instantiate(this.marioCloud, startPoint, Quaternion.identity);
				newObject.transform.parent = this.instantiatedClouds.transform;
				GameObject.DontDestroyOnLoad(newObject);
				newObject.rigidbody2D.AddForce(new Vector2(-60f * (float)(z+1),0f));
				//newObject.rigidbody2D.Sleep();

				GameObject newObjectReflect = (GameObject)Instantiate(this.marioCloud, startPoint2, Quaternion.identity);
				newObjectReflect.transform.parent = this.instantiatedClouds.transform;
				GameObject.DontDestroyOnLoad(newObjectReflect);
				newObjectReflect.rigidbody2D.AddForce(new Vector2(-60f * (float)(z+1),0f));
				//newObjectReflect.rigidbody2D.Sleep();
				*/
			//}
		}
	}
}
